package icasue.tools.compile.compilers.jfobjs;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URI;

/**
 * @Author: Qiao Hang
 * @CreateDate: 2020/12/3 下午3:47
 * @UpdateDate:
 * @Description:
 */
public class JavaFileObjectGen extends AbstractJavaFileObjects {

    private String source;
    private String simpleName;
    private String fullName;
    private ByteArrayOutputStream outputStream;

    public JavaFileObjectGen(String name, String source) {
        super(URI.create("String:///" + name + Kind.SOURCE.extension), Kind.SOURCE);
        this.source = source;
    }

    public JavaFileObjectGen(String name, Kind kind){
        super(URI.create("String:///" + name + kind.extension), kind);
        //after compiler, source will be released.
        this.source = null;
    }

    @Override
    public OutputStream openOutputStream() throws IOException {
        outputStream = new ByteArrayOutputStream();
        return outputStream;
    }

    @Override
    public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
        return this.source;
    }

    @Override
    public byte[] getCompiledBytes() {
        return outputStream.toByteArray();
    }
}
